//
//  KNNormalCell.m
//  Backhome
//
//  Created by kwep_vbn on 2017/3/31.
//  Copyright © 2017年 SmartHome. All rights reserved.
//

#import "KNCommonCell.h"
#import "Masonry.h"
#import "UILabel+KNConvenient.h"
#import "KNMacroFunctions.h"
#import "UIColor+KNAddtion.h"
#import "NSBundle+KNBase.h"

#define kTextViewWidth (KNSCREENWIDTH - 14 * 2)
#define kMinContextHeight 32

@interface KNCommonCell ()<UITextFieldDelegate,UITextViewDelegate>

@end

@implementation KNCommonCell

+ (instancetype)configCellWithType:(KNNormalCellType)type tableView:(UITableView *)table {
    NSString *identifier = [NSString stringWithFormat:@"commonCellType-%ld",type];
    KNCommonCell *cell = [table dequeueReusableCellWithIdentifier:identifier];
    if (cell) {
        return cell;
    }
    if (type == KNNormalCellTypeEmpty) {
        return [[KNCommonCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    else if (type == KNNormalCellTypeTitle) {
        return [self createTitleCell:identifier];
    } else if (type == KNNormalCellTypeCenterTitle) {
        return [self createCenterTitleCell:identifier];
    } else if (type == KNNormalCellTypeCenterTitleButton) {
        return [self createCenterTitleButtonCell:identifier];
    } else if (type == KNNormalCellTypeHTitleValue) {
        return [self createHTitleValueCell:identifier];
    } else if (type == KNNormalCellTypeHTitleValueImageView) {
        return [self createHtitleValueImageViewCell:identifier];
    } else if (type == KNNormalCellTypeVTitleValue) {
        return [self createVTitleValueCell:identifier];
    } else if (type == KNNormalCellTypeChooseButtonTitle) {
        return [self createChooseButtonTitleCell:identifier];
    } else if (type == KNNormalCellTypeChooseButtonTitleValue) {
        return [self createChooseButtonTitleValueCell:identifier];
    } else if (type == KNNormalCellTypeSwitch) {
        return [self createSwitchCell:identifier];
    } else if (type == KNNormalCellTypeButtonSwitch) {
        return [self createButtonSwitchCell:identifier];
    } else if (type == KNNormalCellTypeTitleTextField) {
        return [self createTitleFieldCell:identifier];
    } else if (type == KNNormalCellTypeTitleTextFieldWidth) {
        return [self createTitleFieldCell:identifier textFieldWidth:KNSCREENWIDTH * 0.40];
    } else if (type == KNNormalCellTypeTitleTextView) {
        return [self createTitleTextViewCell:identifier];
    } else if (type == KNNormalCellTypeVChooseButtonTitleValue) {
        return [self createVChooseButtonTitleValueCell:identifier];
    }
    return nil;
}

+ (instancetype)createTitleCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell.contentView addSubview:cell.titleLabel];
    NSBundle *bundle = [NSBundle KNBaseBundle];
    UIImage *img = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:img];
    [cell.contentView addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell.contentView);
        make.width.equalTo(@(img.size.width));
        make.height.equalTo(@(img.size.height));
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(cell);
        make.top.equalTo(@(12));
        make.bottom.equalTo(@(-12));
        make.left.equalTo(@(14));
        make.right.equalTo(cell.accImageView.mas_left).offset(-14);
    }];
    return cell;
}

+ (instancetype)createCenterTitleCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(cell);
    }];
    return cell;
}

+ (instancetype)createCenterTitleButtonCell:(NSString *)identifier {
    NSBundle *bundle = [NSBundle KNBaseBundle];
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_normal" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_selected" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
    [cell.chooseButton addTarget:cell action:@selector(chooseTaped:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:cell.chooseButton];
    
    
    UIImage *image = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:image];
    [cell addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell);
        make.width.equalTo(@(image.size.width));
        make.height.equalTo(@(image.size.height));
    }];
    
    [cell.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cell).offset(-30);
        make.centerY.equalTo(cell);
        make.width.and.height.equalTo(@25);
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.chooseButton.mas_right).offset(8);
        make.centerY.equalTo(cell);
    }];
    return cell;
}

+ (instancetype)createHtitleValueImageViewCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.valueImageView = [[UIImageView alloc] init];
    cell.valueImageView.contentMode = UIViewContentModeScaleAspectFit;
    [cell addSubview:cell.valueImageView];
    
    NSBundle *bundle = [NSBundle KNBaseBundle];
    UIImage *image = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:image];
    [cell addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell);
        make.width.equalTo(@(image.size.width));
        make.height.equalTo(@(image.size.height));
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.equalTo(@(14));
        make.right.equalTo(cell.valueImageView.mas_left).offset(-5);
    }];
    
    [cell.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [cell.valueImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.right.equalTo(cell.accImageView.mas_left).offset(-14);
        make.height.equalTo(cell).multipliedBy(0.8);
        make.width.equalTo(cell.valueImageView.mas_height);
    }];
    return cell;
}

+ (instancetype)createHTitleValueCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.valueLabel = [UILabel KNLabelWithTextColor:[UIColor grayColor] font:13 lineNumbers:1];
    [cell addSubview:cell.valueLabel];
    
    NSBundle *bundle = [NSBundle KNBaseBundle];
    UIImage *img = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:img];
    [cell addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell);
        make.width.equalTo(@(img.size.width));
        make.height.equalTo(@(img.size.height));
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.equalTo(@(14));
        make.right.equalTo(cell.valueLabel.mas_left).offset(-5);
    }];
    
    [cell.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [cell.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.right.equalTo(cell.accImageView.mas_left).offset(-14);
    }];
    return cell;
}

+ (instancetype)createVTitleValueCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell.contentView addSubview:cell.titleLabel];
    
    cell.valueLabel = [UILabel KNLabelWithTextColor:[UIColor grayColor] font:13 lineNumbers:1];
    [cell.contentView addSubview:cell.valueLabel];
    
    NSBundle *bundle = [NSBundle KNBaseBundle];
    UIImage *img = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:img];
    [cell.contentView addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell);
        make.width.equalTo(@(img.size.width));
        make.height.equalTo(@(img.size.height));
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@14);
        make.left.equalTo(@(14));
        make.right.equalTo(cell.accImageView.mas_left).offset(-14);
    }];
    
    [cell.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.titleLabel.mas_bottom).offset(5);
        make.left.and.right.equalTo(cell.titleLabel);
        make.bottom.equalTo(@(-14));
    }];
    
    return cell;
}

+ (instancetype)createChooseButtonTitleCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell.contentView addSubview:cell.titleLabel];
    NSBundle *bundle = [NSBundle KNBaseBundle];
    cell.chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_normal" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_selected" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
    [cell.chooseButton addTarget:cell action:@selector(chooseTaped:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:cell.chooseButton];
    
    UIImage *img = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:img];
    [cell.contentView addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell.contentView);
        make.width.equalTo(@(img.size.width));
        make.height.equalTo(@(img.size.height));
    }];
    
    [cell.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@14);
        make.centerY.equalTo(cell.contentView);
        make.width.and.height.equalTo(@25);
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(cell.chooseButton.mas_right).offset(14);
        make.right.equalTo(cell.accImageView.mas_left).offset(-14);
        make.top.equalTo(cell.contentView).offset(14);
        make.bottom.equalTo(cell.contentView).offset(-14);
    }];
    
    return cell;
}

+ (instancetype)createChooseButtonTitleValueCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.valueLabel = [UILabel KNLabelWithTextColor:[UIColor grayColor] font:13 lineNumbers:1];
    [cell addSubview:cell.valueLabel];
    NSBundle *bundle = [NSBundle KNBaseBundle];
    cell.chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_normal" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_selected" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
    [cell.chooseButton addTarget:cell action:@selector(chooseTaped:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:cell.chooseButton];
    
    UIImage *img = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:img];
    [cell addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell);
        make.width.equalTo(@(img.size.width));
        make.height.equalTo(@(img.size.height));
    }];
    
    
    [cell.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@14);
        make.centerY.equalTo(cell);
        make.width.and.height.equalTo(@25);
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.equalTo(cell.chooseButton.mas_right).offset(14);
        make.right.equalTo(cell.valueLabel.mas_left).offset(-5);
    }];
    
    [cell.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [cell.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.right.equalTo(cell.accImageView.mas_left).offset(-14);
    }];
    return cell;
}

+ (instancetype)createVChooseButtonTitleValueCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.valueLabel = [UILabel KNLabelWithTextColor:[UIColor grayColor] font:13 lineNumbers:1];
    [cell addSubview:cell.valueLabel];
    NSBundle *bundle = [NSBundle KNBaseBundle];
    cell.chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_normal" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];
    [cell.chooseButton setImage:[UIImage imageNamed:@"kn_choose_selected" inBundle:bundle compatibleWithTraitCollection:nil] forState:UIControlStateSelected];
    [cell.chooseButton addTarget:cell action:@selector(chooseTaped:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:cell.chooseButton];

    UIImage *img = [UIImage imageNamed:@"kn_arrow_right_dark" inBundle:bundle compatibleWithTraitCollection:nil];
    cell.accImageView = [[UIImageView alloc] initWithImage:img];
    [cell addSubview:cell.accImageView];
    
    [cell.accImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(-14));
        make.centerY.equalTo(cell);
        make.width.equalTo(@(img.size.width));
        make.height.equalTo(@(img.size.height));
    }];
    
    [cell.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@14);
        make.centerY.equalTo(cell);
        make.width.and.height.equalTo(@25);
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell).offset(12);
        make.left.equalTo(cell.chooseButton.mas_right).offset(14);
        make.right.equalTo(cell.accImageView.mas_left).offset(-5);
    }];
    
    [cell.titleLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    
    [cell.valueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.titleLabel.mas_bottom).offset(5);
        make.left.equalTo(cell.titleLabel);
        make.right.equalTo(cell.accImageView.mas_left).offset(-5);
    }];
    
    return cell;
}

+ (instancetype)createSwitchCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.switchControl = [[UISwitch alloc] init];
    [cell.switchControl addTarget:cell action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    cell.switchControl.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
    [cell addSubview:cell.switchControl];
    
    [cell.switchControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-14);
        make.centerY.equalTo(cell);
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.equalTo(@(14));
        make.right.equalTo(cell.switchControl.mas_left).offset(-14);
    }];
    return cell;
}

+ (instancetype)createButtonSwitchCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.chooseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    NSBundle *bundle = [NSBundle KNBaseBundle];
    UIImage *normalImg = [UIImage imageNamed:@"kn_choose_normal" inBundle:bundle compatibleWithTraitCollection:nil];
    UIImage *selectedImg = [UIImage imageNamed:@"kn_choose_selected" inBundle:bundle compatibleWithTraitCollection:nil];
    
    [cell.chooseButton setImage:normalImg forState:UIControlStateNormal];
    [cell.chooseButton setImage:selectedImg forState:UIControlStateSelected];
    
    [cell.chooseButton addTarget:cell action:@selector(chooseTaped:) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:cell.chooseButton];
    
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.switchControl = [[UISwitch alloc] init];
    [cell.switchControl addTarget:cell action:@selector(switchValueChanged:) forControlEvents:UIControlEventValueChanged];
    cell.switchControl.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);
    [cell addSubview:cell.switchControl];
    
    [cell.switchControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-14);
        make.centerY.equalTo(cell);
    }];
    
    [cell.chooseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@14);
        make.centerY.equalTo(cell);
        make.width.and.height.equalTo(@25);
    }];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.equalTo(cell.chooseButton.mas_right).offset(14);
        make.right.equalTo(cell.switchControl.mas_left).offset(-14);
    }];
    return cell;
}

+ (instancetype)createTitleFieldCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.textField = [[KNTextField alloc] init];
    cell.textField.font = KNFont(14);
    cell.textField.delegate = cell;
    UIColor *placeColor = [UIColor KNColorWithHexRGBA:0x888888];
    [cell.textField setEditTextFieldPlaceholder:@"请输入内容" color:placeColor];
    [cell.textField setReturnKeyType:UIReturnKeyDone];
    [cell.textField addTarget:cell action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    cell.textField.layer.cornerRadius= 5.0f;
    cell.textField.font = KNFont(14);
    cell.textField.textColor = [UIColor blackColor];
    [cell addSubview:cell.textField];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.equalTo(@(14));
    }];
    
    [cell.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell.titleLabel);
        make.left.equalTo(cell.titleLabel.mas_right).offset(20);
        make.right.equalTo(cell).offset(-14);
        make.height.equalTo(@35);
    }];
    
    [cell.titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    return cell;
}

+ (instancetype)createTitleFieldCell:(NSString *)identifier textFieldWidth: (CGFloat)textFieldWidth {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.textField = [[KNTextField alloc] init];
    cell.textField.font = KNFont(14);
    cell.textField.delegate = cell;
    UIColor *placeColor = [UIColor KNColorWithHexRGBA:0x888888];
    [cell.textField setEditTextFieldPlaceholder:@"请输入内容" color:placeColor];
    [cell.textField setReturnKeyType:UIReturnKeyDone];
    [cell.textField addTarget:cell action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    cell.textField.layer.cornerRadius= 5.0f;
    cell.textField.font = KNFont(14);
    cell.textField.textColor = [UIColor blackColor];
    cell.textField.textAlignment = NSTextAlignmentRight;
    [cell addSubview:cell.textField];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell);
        make.left.equalTo(@(14));
    }];
    
    [cell.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(cell.titleLabel);
        make.left.equalTo(cell.titleLabel.mas_right).offset(20);
        make.right.equalTo(cell).offset(-14);
        make.width.equalTo(@(textFieldWidth));
        make.height.equalTo(@35);
    }];
    
    [cell.titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    return cell;
}


+ (instancetype)createTitleTextViewCell:(NSString *)identifier {
    KNCommonCell *cell = [[[self class] alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    cell.titleLabel = [UILabel KNLabelWithTextColor:[UIColor blackColor] font:15 lineNumbers:1];
    [cell addSubview:cell.titleLabel];
    
    cell.textView = [[KNTextView alloc] init];
    cell.textView.scrollEnabled = NO;
    //    cell.textView.textContainerInset = UIEdgeInsetsMake(5, 0, 3, 0);
    cell.textView.font = KNFont(14);
    cell.textView.delegate = cell;
    UIColor *placeColor = [UIColor KNColorWithHexRGBA:0x888888];
    [cell.textView setMyPlaceholder:@"请输入内容"];
    [cell.textView setMyPlaceholderColor:placeColor];
    
    cell.textView.layer.cornerRadius= 5.0f;
    cell.textView.font = KNFont(14);
    cell.textView.textColor = [UIColor blackColor];
    cell.textView.layer.borderColor = [UIColor KNColorWithHexRGBA:0x888888].CGColor;
    cell.textView.layer.borderWidth = 0.5;
    [cell addSubview:cell.textView];
    
    [cell.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@14);
        make.left.equalTo(@(14));
        make.right.equalTo(cell).offset(-14);
    }];
    
    [cell.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.titleLabel.mas_bottom).offset(10);
        make.left.and.right.equalTo(cell.titleLabel);
        make.height.equalTo(@kMinContextHeight);
        make.bottom.equalTo(@(-14));
    }];
    return cell;
}

- (void)configTextViewText:(NSString *)text {
    self.textView.text = text;
    [self setLineSpacing:5 forTextView:self.textView];
    
    CGSize newSize = [self.textView sizeThatFits:CGSizeMake(kTextViewWidth, CGFLOAT_MAX)];
    if (newSize.height < kMinContextHeight) {
        newSize.height = kMinContextHeight;
    }
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(newSize.height));
    }];
    
    [self.textView updateConstraintsIfNeeded];
    [self.textView layoutIfNeeded];
    [self setNeedsDisplay];
}

#pragma mark - Notification

- (void)textViewDidChange:(KNTextView *)textView {
    
    NSInteger loc = textView.selectedRange.location;
    UITextRange *selectedRange = [textView markedTextRange];
    //获取高亮部分
    UITextPosition *pos = [textView positionFromPosition:selectedRange.start offset:0];
    //如果在变化中是高亮部分在变，就不计算字符
    if (selectedRange && pos) {
        return;
    }
    
    [self setLineSpacing:5 forTextView:textView];
    if (self.delegate && [self.delegate respondsToSelector:@selector(commonCell:textViewDidChangeText:)]) {
        [self.delegate commonCell:self textViewDidChangeText:textView.text];
    }
    
    CGSize newSize = [textView sizeThatFits:CGSizeMake(kTextViewWidth, CGFLOAT_MAX)];
    if (newSize.height < kMinContextHeight) {
        newSize.height = kMinContextHeight;
    }
    [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(newSize.height));
    }];
    [self layoutIfNeeded];
    
    textView.selectedRange = NSMakeRange(loc, 0);
    // 让 table view 重新计算高度
    UITableView *tableView = self.tableView;
    if (!tableView) {
        NSLog(@"必须传入tableView");
    }
    [tableView beginUpdates];
    [tableView endUpdates];
    
}

- (void)setLineSpacing:(CGFloat)spacing forTextView:(KNTextView *)textView {
    //    textview 改变字体的行间距
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = spacing;// 字体的行间距
    NSDictionary *attributes = @{
                                 NSFontAttributeName:textView.font,
                                 NSParagraphStyleAttributeName:paragraphStyle,
                                 NSForegroundColorAttributeName:textView.textColor
                                 };
    textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - textfield delegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(commonCell:textFieldDidEndText:)]) {
        [self.delegate commonCell:self textFieldDidEndText:textField.text];
    }
}

- (void)textFieldDidChange:(UITextField *)textField {
    if (self.delegate && [self.delegate respondsToSelector:@selector(commonCell:textFieldDidChangeText:)]) {
        [self.delegate commonCell:self textFieldDidChangeText:textField.text];
    }
}

#pragma mark - action

- (void)chooseTaped:(UIButton *)button {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    if (self.delegate && [self.delegate respondsToSelector:@selector(commonCell:choooseButtonTaped:)]) {
        [self.delegate commonCell:self choooseButtonTaped:button];
    }
}

- (void)switchValueChanged:(UISwitch *)switchControl {
    NSLog(@"%@",NSStringFromSelector(_cmd));
    if (self.delegate && [self.delegate respondsToSelector:@selector(commonCell:switchChanged:)]) {
        [self.delegate commonCell:self switchChanged:switchControl];
    }
}


- (void)updateTitleLabelLeftConstant:(CGFloat)constant {
    if (self.chooseButton) {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.chooseButton.mas_right).offset(constant);
        }];
    } else {
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(constant));
        }];
    }
    
}

- (void)updateTitleLabelTopConstant:(CGFloat)constant {
    
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(constant));
    }];
//    if (self.chooseButton) {
//        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self.chooseButton).offset(constant);
//        }];
//    } else {
//        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(@(constant));
//        }];
//    }
}

- (void)updateTitleLabelBottomConstant:(CGFloat)constant {
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@(constant));
    }];
//    if (self.chooseButton) {
//        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(self.chooseButton).offset(constant);
//        }];
//    } else {
//        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.bottom.equalTo(@(constant));
//        }];
//    }
}

- (void)updateTextFilePlaceholder:(NSString *)placeHolder color:(UIColor *)color {
    [self.textField setEditTextFieldPlaceholder:placeHolder color:color];
}

- (void)updateTextFieldWidthConstant:(CGFloat)constant {
    [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(constant));
    }];
}

- (void)updateTitleLabelFont:(NSInteger)font {
    self.titleLabel.font = KNFont(font);
}

- (void)updateValueLabelFont:(NSInteger)font {
    self.valueLabel.font = KNFont(font);
}

- (void)updateValueLabelBFont:(NSInteger)bfount {
    self.valueLabel.font = KNBFont(bfount);
}

- (void)updateValueLabelRightConstant:(CGFloat)constant {
    [self.valueLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.accImageView.mas_left).offset(constant);
    }];
}

- (void)updateChooseButtonLeftConstant:(CGFloat)constant {
    [self.chooseButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(constant));
    }];
}

- (void)updateChooseButtonCenterXConstant:(CGFloat)constant {
    [self.chooseButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self).offset(constant);
    }];
}

- (void)updateChooseButtonSize:(CGSize)size {
    [self.chooseButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(size.width));
        make.height.equalTo(@(size.height));
    }];
}

- (void)updateAccImageViewHidden:(BOOL)hidden {
    self.accImageView.hidden = hidden;
    
}

- (void)updateAccImageViewRightConstant:(CGFloat)constant {
    [self.accImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(constant));
    }];
}

- (void)updateTextFieldRightConstant:(CGFloat)constant {
    [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(constant));
    }];
}

- (void)updateTextFieldLeftConstant:(CGFloat)constant {
    [self.textField mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(constant));
    }];
}

- (void)updateValueImageViewWidthConstant:(CGFloat)constant {
    [self.valueImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(constant));
    }];
}

- (void)updateValueImageViewHeightConstant:(CGFloat)constant {
    [self.valueImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(constant));
    }];
}


- (void)updateAccImageViewSize:(CGSize)size {
    [self.accImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(size.width));
        make.height.equalTo(@(size.height));
    }];
}


@end
